//
//  AWCPickView.m
//  AntsExpress
//
//  Created by slccper on 16/1/1.
//  Copyright © 2016年 slccper. All rights reserved.
//

#import "AntsPickView.h"

#define TOOLBARHEIGHT 40

@interface AntsPickView()<UIPickerViewDelegate,UIPickerViewDataSource>

@property(nonatomic,copy)CallBack closecallback;
@property(nonatomic,strong)UIPickerView *pickView;
@property(nonatomic,strong)UIToolbar * toolBar;
@property(nonatomic,strong)UIDatePicker *datePicker;
@property(nonatomic,strong)NSArray * dataList;

@end


@implementation AntsPickView

-(id)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if(self){
        self.backgroundColor=[[UIColor alloc] initWithWhite:0 alpha:0.5];
        
    }
    return self;
}

-(instancetype)initPickviewWithArray:(NSArray *)array{
    self=[super init];
    if(self){
        self.dataList=array;
        self.frame=[UIScreen mainScreen].bounds;
        self.pickView=[[UIPickerView alloc] init];
        self.pickView.backgroundColor=[UIColor whiteColor];
        self.pickView.delegate=self;
        self.pickView.dataSource=self;
        self.pickView.frame=CGRectMake(0, self.frame.size.height+TOOLBARHEIGHT,[UIScreen mainScreen].bounds.size.width,self.pickView.frame.size.height);
        [self addSubview:self.pickView];
        [self setUpToolBar:self.pickView.frame];
        
    }
    return self;
}

-(instancetype)initDatePickWithDate:(NSDate *)defaulDate :(NSDate *)maxDate datePickerMode:(UIDatePickerMode)datePickerMode{
    self=[super init];
    if(self){
        self.frame=[UIScreen mainScreen].bounds;
        self.datePicker = [[UIDatePicker alloc] init];
        self.datePicker.datePickerMode = datePickerMode;
        self.datePicker.backgroundColor=[UIColor whiteColor];
        self.datePicker.date = defaulDate;
//        self.datePicker.maximumDate=[NSDate date];
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *adcomps = [[NSDateComponents alloc] init];
        [adcomps setYear:-100];
        NSDate *date100 = [calendar dateByAddingComponents:adcomps toDate:[NSDate date] options:0];
        self.datePicker.minimumDate=date100;
        self.datePicker.maximumDate=maxDate;
        self.datePicker.frame=CGRectMake(0, self.frame.size.height+TOOLBARHEIGHT,[UIScreen mainScreen].bounds.size.width, self.datePicker.frame.size.height);
        [self addSubview:self.datePicker];
        
        [self setUpToolBar:self.datePicker.frame];
    }
    return self;
}

-(void)setUpToolBar:(CGRect) rect{
    rect.origin.y-=TOOLBARHEIGHT;
    rect.size.height=TOOLBARHEIGHT;
    self.toolBar=[[UIToolbar alloc] initWithFrame:rect];
    self.toolBar.backgroundColor=[UIColor whiteColor];
    self.toolBar.tintColor=[UIColor brownColor];
    
    UIBarButtonItem *lefttem=[[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:@"   %@",@"取消"] style:UIBarButtonItemStylePlain target:self action:@selector(remove)];
    
    UIBarButtonItem *centerSpace=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    UIBarButtonItem *right=[[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:@"%@   ",@"確定"] style:UIBarButtonItemStylePlain target:self action:@selector(doneClick)];
    self.toolBar.items=@[lefttem,centerSpace,right];
    [self addSubview:self.toolBar];

}

-(void)doneClick
{
    NSString * result=@"";
    NSInteger cIndex = 0;
    if(self.pickView){
        cIndex = [self.pickView selectedRowInComponent:0];
        if (self.dataList.count>0) {
            result=self.dataList[cIndex];
        }
    }else if(self.datePicker){
        NSDateFormatter *fmat = [[NSDateFormatter alloc] init];
        [fmat setDateFormat:@"yyyy-MM-dd"];
        result = [fmat stringFromDate:self.datePicker.date];
    }
    self.closecallback(result,cIndex);
    [self remove];
}


-(void)show:(CallBack) pCloseCallback{
    self.closecallback=pCloseCallback;
     [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        if(self.pickView){
            self.pickView.frame=CGRectMake(0, self.frame.size.height-self.pickView.frame.size.height, self.pickView.frame.size.width, self.pickView.frame.size.height);
            CGRect rect=self.pickView.bounds;
            rect.origin.y=self.frame.size.height-self.pickView.frame.size.height-TOOLBARHEIGHT;
            rect.size.height=TOOLBARHEIGHT;
            self.toolBar.frame=rect;
        }else if(self.datePicker){
            self.datePicker.frame=CGRectMake(0, self.frame.size.height-self.datePicker.frame.size.height, self.datePicker.frame.size.width, self.datePicker.frame.size.height);
            CGRect rect=self.datePicker.bounds;
            rect.origin.y=self.frame.size.height-self.datePicker.frame.size.height-TOOLBARHEIGHT;
            rect.size.height=TOOLBARHEIGHT;
            self.toolBar.frame=rect;
        }
    } completion:^(BOOL finished) {
        
    }];

}

-(void)remove{
    [UIView animateWithDuration:0.3 animations:^{
        if(self.pickView){
            self.pickView.frame=CGRectMake(0, self.frame.size.height+TOOLBARHEIGHT, self.pickView.frame.size.width, self.pickView.frame.size.height);
            CGRect rect=self.pickView.bounds;
            rect.origin.y=self.frame.size.height;
            rect.size.height=TOOLBARHEIGHT;
            self.toolBar.frame=rect;
        }else if(self.datePicker){
            self.datePicker.frame=CGRectMake(0, self.frame.size.height+TOOLBARHEIGHT, self.datePicker.frame.size.width, self.datePicker.frame.size.height);
            CGRect rect=self.datePicker.bounds;
            rect.origin.y=self.frame.size.height;
            rect.size.height=TOOLBARHEIGHT;
            self.toolBar.frame=rect;
        }
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];

    
}



#pragma mark piackView 数据源方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.dataList.count;
}

#pragma mark UIPickerViewdelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return self.dataList[row];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
